package cn.wizzer.app.user.modules.service.impl;

import cn.wizzer.app.user.modules.models.UserBankCard;
import cn.wizzer.app.user.modules.models.vo.UserBankCardVO;
import cn.wizzer.app.user.modules.services.UserBankCardService;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.base.model.enums.BizExceptionEnum;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.base.service.CommonDBService;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.ArrayUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.Times;

import java.util.List;

/**
 * @author zl
 * @date 2020/8/25 16:07
 */
@IocBean(args = {"refer:dao"})
@Service(interfaceClass = UserBankCardService.class)
public class UserBankCardServiceImpl extends BaseServiceImpl<UserBankCard> implements UserBankCardService {
    public UserBankCardServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private CommonDBService commonDBService;

    @Override
    public void verifyBankCard(String cardId, String userId) {
        UserBankCard userBankCard = this.fetch(cardId);
        if (userBankCard == null) {
            Result.breakWith(BizExceptionEnum.BANK_CARD_NOT_EXIST);
        }
        if (!Lang.equals(userBankCard.getUserId(), userId)) {
            Result.breakWith(BizExceptionEnum.THE_BANK_CARD_IS_NOT_YOURS);
        }
    }

    @Override
    public Pagination listPage(Integer pageNumber, int pageSize, String searchKeyword, String[] searchDate) {
        // 根据收款人,银行卡号搜索
        Sql sql = Sqls.create("SELECT uc.*,u.nickName,u.realName,u.userName,c.country,b.name AS bank,b.shortName FROM user_bank_card AS uc ");
        Sql countSql = Sqls.create("SELECT count(1) FROM user_bank_card AS uc ");
        String joinSql = "LEFT JOIN sys_bank_lib AS b ON uc.bankLibId = b.id " +
                " LEFT JOIN sys_currency AS c ON b.currencyId = c.id" +
                " LEFT JOIN user AS u on uc.userId = u.id $condition ";
        sql.appendSourceSql(joinSql);
        countSql.appendSourceSql(joinSql);
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(searchKeyword)) {
            cnd.and(Cnd.exps("u.userName", "=", searchKeyword)
                    .or("uc.cardNumber", "=", searchKeyword)
                    .or("u.realName", "=", searchKeyword)
                    .or("b.shortName", "=", searchKeyword)
            );
        }
        if (ArrayUtils.isNotEmpty(searchDate)) {
            cnd.and("uc.ctAt", ">=", Times.d2TS(Times.D(searchDate[0])));
            cnd.and("uc.ctAt", "<=", Times.d2TS(Times.D(searchDate[1])));
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("order by uc.opAt desc");
        countSql.setCondition(cnd);
        return commonDBService.listPage(UserBankCardVO.class, pageNumber, pageSize, sql.toString(), countSql.toString());
    }

    @Override
    public UserBankCardVO getById(String cardId) {
        return commonDBService.fetchSql(UserBankCardVO.class, "select c.*,sc.country,b.name as bank from user_bank_card c" +
                " left join sys_bank_lib b on b.id = c.bankLibId" +
                " left join sys_currency sc on b.currencyId = sc.id" +
                " where c.id = '" + cardId + "'");
    }

    @Override
    public List<UserBankCardVO> queryVO(Cnd cnd) {
        return commonDBService.list(UserBankCardVO.class, "select c.*,if(c.type = 'UPI','UPI',b.name)AS bank,sc.country from user_bank_card c" +
                " left join sys_bank_lib b on b.id = c.bankLibId " +
                " left join sys_currency sc on sc.id = b.currencyId " +
                cnd.toString());
    }
}
